package fake

import (
	"bytes"
	"io"
	"testing"

	"github.com/spf13/cobra"
)

func WithCmd(buf io.Writer) *cobra.Command {
	cmd := &cobra.Command{}
	cmd.SetOut(buf)
	return cmd
}

func TestComplete_Complete(t *testing.T) {
	t.Run("ok", func(sub *testing.T) {
		lines := []string{
			"test",        // first run
			"application", // second run
			"bundle",      // third run
		}
		compl := NewComplete().
			WithComplete(lines...).
			WithExecutes(lines...).
			WithError(nil)

		for i := 0; i < 10; i++ {
			buf := &bytes.Buffer{}
			if err := compl.Complete(WithCmd(buf), []string{"this"}); err != nil {
				sub.Errorf("got error: %v", err)
			}
			if i <= len(lines)-1 {
				result := buf.String()
				expected := lines[i]
				if result != expected {
					sub.Errorf("expected: %v, got: %v", expected, result)
				}
			}
		}
	})
}

func TestComplete_Execute(t *testing.T) {
	NewComplete("test").Execute(WithCmd(&bytes.Buffer{}), []string{"test"})
}
